{extend name="public:base" /}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <!--<div class="ibox-title">
                    <a href="{:url('doMsg')}" class="btn btn-primary btn-lg">发布公告</a>

                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="iconfont icon-chevron-up"></i>
                        </a>
                    </div>
                </div>-->

                <div class="ibox-content">
                    <div class="row search-form">
                        <div class="col-sm-2 m-b-sm">
                            <div class="input-group">
                                <input placeholder="请输入姓名" name="username" value="{:input('username')}" type="text" class="form-control form-control-lg">
                            </div>
                        </div>
                        <div class="col-sm-2 m-b-sm">
                            <div class="input-group">
                                <input placeholder="请输入电话" name="mobile" value="{:input('mobile')}" type="text" class="form-control form-control-lg">
                            </div>
                        </div>
                        <div class="col-sm-2 m-b-sm">
                            <div class="input-group">
                                <button type="button" class="btn btn-primary btn-lg screen-btn">搜索</button>
                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>姓名</th>
                                <th>电话</th>
                                <th>余额</th>
                                <th>积分</th>
                                <th>订单数</th>
                                <th>退货订单数</th>
                                <th>上级用户</th>
                                <th>用户等级</th>
                                <th>状态</th>
                                <th>注册时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {volist name="list" id="vo"}
                            <tr>
                                <td>{$vo.id}</td>
                                <td>{$vo.username}</td>
                                <td>{$vo.mobile}</td>
                                <td>{$vo.balance}</td>
                                <td>{$vo.integral}</td>
                                <td>{$vo.all_order_count}</td>
                                <td>{$vo.return_order_count}</td>
                                <td>{$vo.up_username}</td>
                                <td>{:doUserLevel($vo.level)}</td>
                                <td>{$vo.status == 1 ? '正常' : '禁用'}</td>
                                <td>{:timeFormat($vo.create_time)}</td>
                                <td class="project-actions">
                                    <a href="{:url('doUser', ['id' => $vo.id])}" class="btn btn-white btn-sm">
                                        <i class="iconfont icon-edit"></i> 编辑
                                    </a>
                                    <a href="{:url('doUserStatus', ['id' => $vo.id, 'status' => ($vo.status == 1 ? 2 : 1)])}" class="btn btn-white btn-sm ajax-operate">
                                        <i class="iconfont icon-quanxianpeizhi"></i> {$vo.status == 1 ? '禁用' : '启用'}
                                    </a>
                                    <a href="javascript: void(0)" onclick="addModelOpenLevel({$vo.id}, {$vo.level})" class="btn btn-white btn-sm">
                                        <i class="iconfont icon-change"></i> 级别变更
                                    </a>
                                    <a href="javascript: void(0)" onclick="addModelOpenBalance({$vo.id})" class="btn btn-white btn-sm">
                                        <i class="iconfont icon-change"></i> 余额变更
                                    </a>
                                    <a href="javascript: void(0)" onclick="addModelOpenIntegral({$vo.id})" class="btn btn-white btn-sm">
                                        <i class="iconfont icon-change"></i> 积分变更
                                    </a>
                                    <a href="javascript: void(0)" onclick="chooseUser({$vo.id})" data-toggle="modal" data-target="#myModal2" class="btn btn-white btn-sm">
                                        <i class="iconfont icon-huiyuanqia"></i> 充值
                                    </a>
                                </td>
                            </tr>
                            {/volist}
                            </tbody>
                        </table>
                        {if !empty($list)}{$list->render()}{/if}
                    </div>

                </div>
            </div>
        </div>

    </div>
</div>

<div class="modal inmodal" id="myModal2" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content animated flipInY">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">充值</h4>
            </div>
            <div class="modal-body">
                <input type="number" class="form-control balance" placeholder="请输入充值金额">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white close-model" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="confirmRecharge">确认充值</button>
            </div>
        </div>
    </div>
</div>

<!--  级别变更弹出框  -->
<div class="modal inmodal" id="myModal4" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content animated flipInY">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" onclick="modelCloseLevel()"><span
                        aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title">等级变更</h4>
            </div>
            <div class="modal-body">
                <select class="form-control level">
                    <option value="1">普通用户</option>
                    <option value="2">VIP</option>
                    <option value="3">VVIP</option>
                </select>

                <input type="text" class="form-control reason" maxlength="25" placeholder="请输入变更原因" style="margin-top: 10px">

                <input type="hidden" class="id" value="">
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" onclick="modelCloseLevel()">关闭</button>
                <button type="button" class="btn btn-primary" onclick="confirmLevelChange()">确认</button>
            </div>
        </div>
    </div>
</div>

<!--  余额变更弹出框  -->
<div class="modal inmodal" id="myModal3" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content animated flipInY">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" onclick="modelCloseBalance()"><span
                        aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title">余额变更</h4>
            </div>
            <div class="modal-body">
                <select class="form-control type">
                    <option value="0">请选择变更类型</option>
                    <option value="1">奖励余额</option>
                    <option value="2">处罚余额</option>
                </select>

                <input type="number" class="form-control price" min="0" placeholder="请输入变更数额" style="margin-top: 10px">

                <input type="text" class="form-control reason" maxlength="25" placeholder="请输入变更原因" style="margin-top: 10px">

                <input type="hidden" class="id" value="">
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" onclick="modelCloseBalance()">关闭</button>
                <button type="button" class="btn btn-primary" onclick="confirmBalanceChange()">确认</button>
            </div>
        </div>
    </div>
</div>

<!--  积分变更弹出框  -->
<div class="modal inmodal" id="myModal5" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content animated flipInY">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" onclick="modelCloseIntegral()"><span
                        aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title">积分变更</h4>
            </div>
            <div class="modal-body">
                <select class="form-control type">
                    <option value="0">请选择变更类型</option>
                    <option value="1">充值积分</option>
                    <option value="2">处罚积分</option>
                </select>

                <input type="number" class="form-control integral" min="0" placeholder="请输入变更数额" style="margin-top: 10px">

                <input type="text" class="form-control reason" maxlength="25" placeholder="请输入变更原因" style="margin-top: 10px">

                <input type="hidden" class="id" value="">
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" onclick="modelCloseIntegral()">关闭</button>
                <button type="button" class="btn btn-primary" onclick="confirmIntegralChange()">确认</button>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="js"}
<script>
    var user_id = 0;

    function chooseUser(id) {
        user_id = id;
    }

    $('#confirmRecharge').click(function () {
        var balance = $('.balance').val();
        $.post('/admin/manage/userRecharge', {user_id, balance}, res => {
            if (res.result == 1) {
                layer.msg(res.msg);
                setTimeout(function () {
                    window.location.reload();
                }, 500)
            } else {
                layer.msg(res.msg);
            }
        }, 'JSON')
    })

    /***************************************** 分界线 *****************************************/

    // 打开 级别变更 弹框
    function addModelOpenLevel(id, level) {
        $('body').addClass('modal-open').append('<div class="modal-backdrop show"></div>');
        $('#myModal4').css('display', 'block').addClass('show');

        $('#myModal4 .level').val(level);
        $('#myModal4 .id').val(id);
    }

    // 关闭 级别变更 弹框
    function modelCloseLevel() {
        $('body').removeClass('modal-open');
        $('.modal-backdrop').remove();
        $('#myModal4').css('display', 'none').removeClass('show');

        $('#myModal4 .reason').val('');
        $('#myModal4 .id').val('');
    }

    // 级别变更
    function confirmLevelChange() {
        var id = $('#myModal4 .id').val();
        var level = $('#myModal4 .level').val();
        var reason = $('#myModal4 .reason').val();

        $.post("{:url('changeUserLevel')}", {
            id: id,
            level: level,
            reason: reason
        }, function (res) {
            if (res.result == 1) {
                layer.msg(res.msg);
                setTimeout(function () {
                    window.location.reload();
                }, 500)
            } else {
                layer.msg(res.msg);
            }
        }, 'json')
    }

    /***************************************** 分界线 *****************************************/

    // 打开 余额变更 弹框
    function addModelOpenBalance(id) {
        $('body').addClass('modal-open').append('<div class="modal-backdrop show"></div>');
        $('#myModal3').css('display', 'block').addClass('show');

        $('.id').val(id);
    }

    // 关闭 余额变更 弹框
    function modelCloseBalance() {
        $('body').removeClass('modal-open');
        $('.modal-backdrop').remove();
        $('#myModal3').css('display', 'none').removeClass('show');

        $('#myModal3 .type').val(0);
        $('#myModal3 .price').val('');
        $('#myModal3 .reason').val('');
        $('#myModal3 .id').val('');
    }

    // 余额变更
    var clickCount = 0;
    function confirmBalanceChange() {
        var id = $('#myModal3 .id').val();
        var type = $('#myModal3 .type').val();
        var price = $('#myModal3 .price').val();
        var reason = $('#myModal3 .reason').val();
        if(clickCount == 0){
            ++clickCount;
            $.post("{:url('balanceChange')}", {
                id: id,
                type: type,
                price: price,
                reason: reason
            }, function (res) {
                if (res.result == 1) {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 500)
                } else {
                    clickCount = 0;
                    layer.msg(res.msg);
                }
            }, 'json')
        }
    }

    /***************************************** 分界线 *****************************************/

    // 打开 积分变更 弹框
    function addModelOpenIntegral(id) {
        $('body').addClass('modal-open').append('<div class="modal-backdrop show"></div>');
        $('#myModal5').css('display', 'block').addClass('show');

        $('.id').val(id);
    }

    // 关闭 积分变更 弹框
    function modelCloseIntegral() {
        $('body').removeClass('modal-open');
        $('.modal-backdrop').remove();
        $('#myModal5').css('display', 'none').removeClass('show');

        $('#myModal5 .type').val(0);
        $('#myModal5 .integral').val('');
        $('#myModal5 .reason').val('');
        $('#myModal5 .id').val('');
    }

    // 积分变更
    var clickIntegralCount = 0;
    function confirmIntegralChange() {
        var id = $('#myModal5 .id').val();
        var type = $('#myModal5 .type').val();
        var integral = $('#myModal5 .integral').val();
        var reason = $('#myModal5 .reason').val();
        if(clickIntegralCount == 0){
            ++clickIntegralCount;
            $.post("{:url('integralChange')}", {
                id: id,
                type: type,
                integral: integral,
                reason: reason
            }, function (res) {
                if (res.result == 1) {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 500)
                } else {
                    clickIntegralCount = 0;
                    layer.msg(res.msg);
                }
            }, 'json')
        }
    }
</script>
{/block}